package operator;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

/**
 * 自定义分区器
 */
public class CustomPartitionDemo {

    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<String> maped = lines.map(new RichMapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                int indexOfThisSubtask = getRuntimeContext().getIndexOfThisSubtask();
                return s + " : " + indexOfThisSubtask;
            }
        });

        // 使用自定义分区器
        DataStream<String> partitioned = maped.partitionCustom(new Partitioner<String>() {
            @Override
            public int partition(String s, int i) {
                int res = 0;
                if (s.contains("hadoop")) {
                    res = 1;
                } else if (s.contains("spark")) {
                    res = 2;
                } else if (s.contains("flink")) {
                    res = 3;
                }
                return res;
            }
        }, (KeySelector<String, String>) s -> s);

        // 打印
        partitioned.addSink(new RichSinkFunction<String>() {
            @Override
            public void invoke(String value, Context context) throws Exception {
                int indexOfThisSubtask = getRuntimeContext().getIndexOfThisSubtask();
                System.out.println(value + " -> " + indexOfThisSubtask);
            }
        }).name("mySink");

        env.execute();
    }
}
